﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Client
{
    /// <summary>
    /// File: Client_Handler.cs
    /// Author: James Dibble 10009689, Harry Winser 10016143
    /// Last Revision: 20/03/12
    /// </summary>
    class ClientMessage
    {
        #region Attributes
        private String _messToSend = String.Empty;
        private String _messType;
        #endregion
        /// <summary>
        /// Create the message to send to the Coffee Server
        /// </summary>
        public ClientMessage() { }

        #region Methods
        /// <summary>
        /// Builds a message to send to the Server, without additions list 
        /// </summary>
        /// <param name="method">The type of message I.e. BREW, GET, etc...</param>
        /// <param name="potDes">The Pot you designated to brew your coffee</param>
        /// <returns>Messgae to be sent to the Server</returns>
        public String BuildMessage(MethodType method, String potDes, Boolean Safe = false)
        {
            switch (method)
            {
                case MethodType.BREW:
                    _messType = "BREW ";
                    if (potDes != "/") // checks for no pot designated
                    {
                        _messToSend = _messType + "/" + potDes + " HTCPCP/1.0\r\n";
                    }
                    else
                    {
                        _messToSend = _messType + "/" + " HTCPCP/1.0\r\n";
                    }
                    break;
                case MethodType.GET:
                    _messType = "GET ";
                    if (potDes != "/") // checks for no pot designated
                    {
                        _messToSend = _messType + "/" + potDes + " HTCPCP/1.0\r\n";
                        if (Safe)
                        {
                            _messToSend += "Safe:Yes\r\n";
                        }
                    }
                    else
                    {
                        _messToSend = _messType + "/" + " HTCPCP/1.0\r\n";
                    }
                    break;
                case MethodType.PROPFIND:
                    _messType = "PROPFIND ";
                    if (potDes != "/") // checks for no pot designated
                    {
                        potDes = potDes.Replace("/", "");
                        _messToSend = _messType + "/" + potDes + " HTCPCP/1.0\r\n";
                    }
                    else
                    {
                        _messToSend = _messType + "/" + " HTCPCP/1.0\r\n";
                    }
                    break;
                case MethodType.WHEN:
                    _messType = "WHEN ";
                    if (potDes != "/") // checks for no pot designated
                    {
                        _messToSend = _messType + "/" + potDes + " HTCPCP/1.0\r\n";
                    }
                    else
                    {
                        _messToSend = _messType + "/" + " HTCPCP/1.0\r\n";
                    }
                    break;
            }
            return _messToSend;
        }
        /// <summary>
        /// Builds a complete BREW message to send to the server, if there is an addtions list 
        /// </summary>
        /// <param name="potDes">The Pot you designated to brew your coffee</param>
        /// <param name="strAdditions">The list of your addtions to the cofee</param>
        /// <returns>BREW message to be sent to the Server</returns>
        public String BuildMessage(String potDes, String[] strAdditions) 
        {
            String strReturn = "";
            String strAdditionList = "";
            if(potDes != "/") potDes = "/" + potDes;
            foreach (String strThisAdd in strAdditions) strAdditionList += strThisAdd + ";";
            strAdditionList = strAdditionList.Substring(0, strAdditionList.Length -1);
            strReturn += String.Format("BREW {0} HTCPCP/1.0\r\nAccept-additions:{1}\r\n\r\n",
                                       potDes,
                                       strAdditionList);

            return strReturn;
        }
        #endregion
        /// <summary>
        /// The different Methods Types for request messages.
        /// </summary>
        public enum MethodType
        {
            BREW,
            GET,
            PROPFIND,
            WHEN
        }
    }
}
